﻿using System;
using System.Text;
using System.Data;
using VIT.DataHelper.DataAccess;
using System.Data.SqlClient;

namespace VIT.Business
{
    public class BLCategory
    {
        SQLSupport sqlSupport = new SQLSupport();

        #region Properties
        public int Id { get; set; }
        public string LanguageId { get; set; }
        public string Image { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        #endregion

        #region Methods
        public DataTable GetListChuildCat(int category)
        {
            StringBuilder strQuery = new StringBuilder();
            strQuery.Append("select c.id, cl.Title,Description,Image FROM Category c, Category_Language cl where c.Id=CategoryId and cl.LanguageID=@LanguageID and ParentId = @CategoryId and Published=1 order by Orders desc");
            SqlParameter CategoryId = new SqlParameter("@CategoryId", category);
            SqlParameter LanguageId = new SqlParameter("@LanguageID", this.LanguageId);
            return sqlSupport.ExecuteDataset(false, strQuery.ToString(), LanguageId, CategoryId).Tables[0];
        }

        public void GetSingle()
        {
            if (Exist())
            {
                string strQuery = "select Title, Image, Description, Orders, Published from Category a left join Category_Language on Id=CategoryId and LanguageID=@LanguageId where Id=@Id";
                SqlParameter Id = new SqlParameter("@Id", this.Id);
                SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
                DataTable dt = sqlSupport.ExecuteDataset(false, strQuery, Id, LanguageId).Tables[0];

                if (dt.Rows.Count > 0)
                {
                    this.Image = dt.Rows[0]["Image"].ToString();
                    this.Title = dt.Rows[0]["Title"].ToString();
                    this.Description = dt.Rows[0]["Description"].ToString();
                }
            }
            else this.Id = -1;
        }

        private bool Exist()
        {
            string strQuery = "select Id from Category where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", this.Id);
            return sqlSupport.ExecuteScalar(false, strQuery, Id) != null;
        }
        #endregion
    }
}
